finp$="video.mp4"

open finp$+".bin" for output as #1

'- numeric values into strings: empty strings means no command line entering - ffmpeg time set in seconds
fps$="6":timestart$="":timedura$="":gifdelay$="15"


'#! /usr/bin/sdlbrt
'finp$="RGB_24bits_palette_sample_image_b2.jpg"

dim dpvl[8]
function hexvl(a$)
   e=asc(ucase$(a$))
   hexvl=abs(((e>=48 and e<=57)*(e-48))+((e>=65 and e<=70)*(e-55)))
 end function
sub hexvpr(a$)
     lngta=len(a$):i=1
  while i<=lngta
     m1$=mid$(a$,i,1):m2$=mid$(a$,i+1,1)
     b=(hexvl(m1$)*16)+hexvl(m2$):WriteByte(1,b):i+=2
   end while
 end sub
function bthx$(a)
  b$="00"+hex$(a):bthx$=right$(b$,2)
 end function



clstr=0 '- 0..3
xed=40:yed=36:wdwd=1
setdisplay(xed*4,yed*4,32,wdwd):paper(8^8-1):ink(0):pen(0):cls:ftempref$="_tmpr"

'- shell:  ffmpeg -i teste01.flv -r 6 -s 40x36 -ss 48 -t 5 _tmpr%d.png
shelout$="ffmpeg -i "+finp$
if fps$<>"" then: shelout$=shelout$+" -r "+fps$: end if
shelout$=shelout$+" -s 40x36"
if timestart$<>"" then: shelout$=shelout$+" -ss "+timestart$: end if
if timedura$<>"" then: shelout$=shelout$+" -t "+timedura$: end if
shelout$=shelout$+" "+ftempref$+"%d.png"
setcaption("ffmpeg: converting..."):shell(shelout$)
frst=0

'- counting frames
frm=1
while fileexists(ftempref$+str$(frm)+".png")<>0
  setcaption("counting frames: "+str$(frm)):frm=frm+1
wend


for frq=1 to frm-1:setcaption("frame "+str$(frq)+"/"+str$(frm-1))
framenm$=ftempref$+str$(frq)+".png"
'loadimage(framenm$,1):pasteicon(0,0,1)



ink(0x000000):dot( 0,0)
ink(0x000000):dot( 1,0)
ink(0x3EB849):dot( 2,0)
ink(0x74D07D):dot( 3,0)
ink(0x5955E0):dot( 4,0)
ink(0x8076F1):dot( 5,0)
ink(0xB95E51):dot( 6,0)
ink(0x65DBEF):dot( 7,0)
ink(0xDB6559):dot( 8,0)
ink(0xFF897D):dot( 9,0)
ink(0xCCC35E):dot(10,0)
ink(0xDED087):dot(11,0)
ink(0x3AA241):dot(12,0)
ink(0xB766B5):dot(13,0)
ink(0xCCCCCC):dot(14,0)
ink(0xFFFFFF):dot(15,0)
grab(1,0,0,16,1):saveimage("_.bmp",1):shell("convert _.bmp _t9.gif && rm _.bmp" )



'shell("rm _t*.png")
shell("convert "+framenm$+" -scale "+str$(xed*4)+"x"+str$(yed*4)+"! _t2.png")
shell("convert "+framenm$+" +dither -colors 16 _t4.png")
shell("convert _t4.png +dither -posterize 8 _t5.png")
shell("convert _t5.png +dither -colors 16 _t6.png")
'shell("convert _t2.png -quantize CMY -remap _t6.png -dither Riemersma _t3.png")
shell("convert _t2.png -quantize CMY -remap _t9.gif -dither Riemersma _t3.png")
shell("convert _t3.png -unique-colors -scale 400% _t4.png")




loadimage("_t3.png",1):pasteicon(0,0,1)

dim p1[16],k1[16]

function grayscale(ct):
  cq=ct
  bq=bitwiseand(cq,255):cq=int(cq/256)
  gq=bitwiseand(cq,255):cq=int(cq/256)
  rq=bitwiseand(cq,255):cq=int(cq/256)
  return int(((rq*30)+(gq*59)+(bq*11))/100)
  end function

for y2=0 to yed-1
  setcaption(str$(int(100*(y2/yed)))+"%")
  for x2=0 to xed-1
    for y1=0 to 3:for x1=0 to 3
      xq=y1*4+x1
      p1[xq]=point(x2*4+x1,y2*4+y1)
      next:next
    for i=0 to 15
      k1[i]=grayscale(p1[i])
      next
    for j=0 to 14:for i=0 to 14
      if k1[i]>k1[i+1] then:
        tm=k1[i]:k1[i]=k1[i+1]:k1[i+1]=tm
        tm=p1[i]:p1[i]=p1[i+1]:p1[i+1]=tm
        end if
      next:next

    if clstr=0 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 6]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 9]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[10]):dot(x2*4+1,y2*4+1)
    ink(p1[ 3]):dot(x2*4+2,y2*4+1)
    ink(p1[13]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[ 7]):dot(x2*4+3,y2*4+2)
    ink(p1[ 2]):dot(x2*4+0,y2*4+3)
    ink(p1[12]):dot(x2*4+1,y2*4+3)
    ink(p1[ 5]):dot(x2*4+2,y2*4+3)
    ink(p1[11]):dot(x2*4+3,y2*4+3)
    end if
    
    if clstr=1 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 4]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 8]):dot(x2*4+2,y2*4+0)
    ink(p1[12]):dot(x2*4+3,y2*4+0)
    ink(p1[10]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 2]):dot(x2*4+2,y2*4+1)
    ink(p1[ 6]):dot(x2*4+3,y2*4+1)
    ink(p1[ 1]):dot(x2*4+0,y2*4+2)
    ink(p1[ 5]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[11]):dot(x2*4+0,y2*4+3)
    ink(p1[15]):dot(x2*4+1,y2*4+3)
    ink(p1[ 3]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=2 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[10]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 1]):dot(x2*4+2,y2*4+0)
    ink(p1[11]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 5]):dot(x2*4+2,y2*4+1)
    ink(p1[15]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[ 2]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[ 3]):dot(x2*4+3,y2*4+2)
    ink(p1[12]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[13]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=3 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[12]):dot(x2*4+1,y2*4+0)    
    ink(p1[ 3]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 8]):dot(x2*4+0,y2*4+1)
    ink(p1[ 4]):dot(x2*4+1,y2*4+1)
    ink(p1[11]):dot(x2*4+2,y2*4+1)
    ink(p1[ 7]):dot(x2*4+3,y2*4+1)
    ink(p1[ 2]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[10]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[ 9]):dot(x2*4+2,y2*4+3)
    ink(p1[ 5]):dot(x2*4+3,y2*4+3)
    end if

    x1=x2 mod 4:y1=y2 mod 4
    u=point(x2*4+x1,y2*4+y1)
    ink(u):dot(x2,y2)
    next:next

'loadimage("_t4.png",1):pasteicon(xed,0,1)
'shell("rm _t*.png")
'shell("rm _t*.gif")


ink(0x000000):bar( 40,0, 44,4)
ink(0x000000):bar( 44,0, 48,4)
ink(0x3EB849):bar( 48,0, 52,4)
ink(0x74D07D):bar( 52,0, 56,4)
ink(0x5955E0):bar( 56,0, 60,4)
ink(0x8076F1):bar( 60,0, 64,4)
ink(0xB95E51):bar( 64,0, 68,4)
ink(0x65DBEF):bar( 68,0, 72,4)
ink(0xDB6559):bar( 72,0, 76,4)
ink(0xFF897D):bar( 76,0, 80,4)
ink(0xCCC35E):bar( 80,0, 84,4)
ink(0xDED087):bar( 84,0, 88,4)
ink(0x3AA241):bar( 88,0, 92,4)
ink(0xB766B5):bar( 92,0, 96,4)
ink(0xCCCCCC):bar( 96,0,100,4)
ink(0xFFFFFF):bar(100,0,104,4)



'-------------------------------------------------------------------------------

for y2=0 to yed-1
  setcaption(str$(int(100*(y2/yed)))+"%")
  for x2=0 to xed-1
    u=point(x2,y2)
    for c2=15 to 0 step -1
      cp=point(xed+c2*4,0)
      if (u=cp) then:

        ink(0xFFFFFF)
        if bitwiseand(c2,1)<>0 then:ink(0):end if
        dot((xed*0)+x2,yed+y2)

        ink(0xFFFFFF)
        if bitwiseand(c2,2)<>0 then:ink(0):end if
        dot((xed*1)+x2,yed+y2)

        ink(0xFFFFFF)
        if bitwiseand(c2,4)<>0 then:ink(0):end if
        dot((xed*2)+x2,yed+y2)

        ink(0xFFFFFF)
        if bitwiseand(c2,8)<>0 then:ink(0):end if
        dot((xed*3)+x2,yed+y2)

        end if
      next
    next:next


'-------------------------------------------------------------------------------



for y0=0 to 35 step 8
  for x0=0 to 39 step 2
    for y1=0 to 7
      c0=0:c1=0
      for cq=0 to 15
        cdp=point(x0,y0+y1)
        cqp=point(40+(cq*4),0)
        if cdp=cqp then:c0=cq:end if
        next
      for cq=0 to 15
        cdp=point(x0+1,y0+y1)
        cqp=point(40+(cq*4),0)
        if cdp=cqp then:c1=cq:end if
        next
      writebyte(1,c0*16+c1)
      next:next:next




'-------------------------------------------------------------------------------

'open finp$+".hex" for output as #1:open finp$+".bin" for input as #2
'  print #1,"goto bin01end"
'  print #1,"bin01:"
'  print #1,"asm"
'  a$="  defb "
'   while not(eof(2))
'        aln=len(a$)
'     if aln>=68 then
'        tzl=len(a$):a$=left$(a$,tzl-1):print #1,a$:a$="  defb "
'      end if
'     u=readbyte(2)
'     a$=a$+"$"+bthx$(u):a$=a$+","
'   end while
'        aln=len(a$)
'     if aln>0 then
'        tzl=len(a$):a$=left$(a$,tzl-1):print #1,a$:a$="  defb "
'      end if
'  print #1,"  end asm"
'  print #1,"bin01end:"
' close #1:close #2

'-------------------------------------------------------------------------------

grab (1,0,0,xed,yed):saveimage(ftempref$+str$(frq)+"_out.bmp",1)
shell("convert "+ftempref$+str$(frq)+"_out.bmp _tmpr.gif")

gifdelv=val(gifdelay$)

  if frst<>0 then:
    shell("gifsicle -l=forever -d="+str$(gifdelv)+" _tmpr1.gif _tmpr.gif > _tmpr2.gif")
  else
    shell("gifsicle -l=forever -d="+str$(gifdelv)+" _tmpr.gif > _tmpr2.gif"):frst=1
    end if

'shell("rm "+ftempref$+str$(frq)+"_out.bmp "+ftempref$+str$(frq)+".png")
'shell("rm _tmpr.gif _tmpr1.gif")
shell("mv _tmpr2.gif _tmpr1.gif")
next
shell("mv _tmpr1.gif "+finp$+".gif"):shell("gthumb "+finp$+".gif")


'waitkey

'grab(1,0,0,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+".png && rm _.bmp" )

'shell("rm _t*.*")

'shell("convert "+finp$+".png  -colors 16 -compress None "+finp$+".ilbm ")
'shell("convert "+finp$+".png _.ppm && ppmtopi1 _.ppm > "+finp$+".pi1 && rm _.ppm")

'waitkey

close #1




